import 'dart:async';

import 'package:core_foundation_example/pages/map/Page_map_home.dart';
import 'package:core_foundation_example/pages/page_home.dart';
import 'package:core_foundation_example/pages/page_login.dart';
import 'package:core_foundation_example/pages/page_permission_test.dart';
import 'package:core_foundation_example/pages/style/style_app.dart';
import 'package:core_foundation_example/pages/utils/util_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:redux/redux.dart';
import 'package:core_foundation/redux/state_app.dart';
import 'package:core_foundation/model/model_user.dart';
import 'package:core_foundation/utils/utils_common.dart';
import 'package:core_foundation/utils/utils_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:core_foundation/net/http_manager.dart';

class FlutterReduxApp extends StatefulWidget {
  @override
  _FlutterReduxAppState createState() => _FlutterReduxAppState();
}

class _FlutterReduxAppState extends State<FlutterReduxApp> {
  final store = Store<StateApp>(reducerAppState,
      initialState: StateApp(
          user: User.empty(),
          themeData: UtilsCommon.getThemeData(Colors.blueGrey)));

  @override
  Widget build(BuildContext context) {
    return StoreProvider<StateApp>(
      store: store,
      child: StoreBuilder<StateApp>(
        builder: (context, store) {
          return MaterialApp(
            theme: store.state.themeData,
            routes: {
//              "/" : (context) => PageLogin(),
//              PageWelcome.rName : (context) => PageWelcome(),
              PageHome.rName: (context) => PageHome(),
//              PagePermissionTest.rName: (context) => PagePermissionTest(),
//              CameraExampleHome.rName : (context) => CameraExampleHome()
            },
            home: HomeContent(),
          );
        },
      ),
    );
  }
}

class HomeContent extends StatefulWidget {
  @override
  _HomeContentState createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  final Permission cameraPermission = Permission.camera;
  final Permission storagePermission = Permission.storage;

  PermissionStatus cameraStatus, storageStatus;

  @override
  void initState() {
    super.initState();
//    ScreenUtil.init(context);
//    ScreenUtil().setSp(StyleApp.FONT_CONTENT);
    _initMap();
//    Future.delayed(Duration(microseconds: 1), _initMap());
  }

  _initMap() async {
    await AmapService.init(
        iosKey: '7a***********************f4',
        androidKey: '42d8d4eb257a743e85efd3a59ee4155c');

    /// 如果你觉得引擎的日志太多, 可以关闭Fluttify引擎的日志
    await enableFluttifyLog(false); // 关闭log
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(5.0),
              child: FlatButton(
                  onPressed: () => Navigator.pushNamed(context, PageHome.rName),
                  child: Text("首页")),
            ),
            Padding(
              padding: EdgeInsets.all(5.0),
              child: FlatButton(
                  onPressed: () => Navigator.push(context,
                      MaterialPageRoute(builder: (context) => PageMapHome())),
//                      Navigator.pushNamed(context, PagePermissionTest.rName),

                  child: Text("高德地图")),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: FlatButton(
                  onPressed: () async {
                    var state = await checkStatus();
                    if (state) {
//                      _goCamera(context);

                    }
                  },
                  child: Text("相机")),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: FlatButton(
                  onPressed: () {
                    UtilsDialog.showSingleNoticeDialog(
                        context, "sjdflaskjdflkjasdlfj");
                  },
                  child: Text("Dialog")),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: FlatButton(
                  onPressed: () => Fluttertoast.showToast(
                      msg:
                          "浮动按钮"), //Navigator.push(context, MaterialPageRoute(builder: (context)=>TestPageHome())),
                  child: Text("Drawer 首页")),
            ),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: FlatButton(
                  onPressed: () => Navigator.push(context,
                      MaterialPageRoute(builder: (context) => PageLogin())),
                  child: Text("登录")),
            ),
            _renderItem("Logger插件", onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context)=> PageUtilTest()))),
            _renderItem("没有网络拦截", onPressed: () async {
              debugPrint("开始网络请求");
              var res = await HttpManager().get("https://www.baidu.com");
              if (null != res) {

              }
            })
          ],
        ));
    
  }

  Widget _renderItem(label, {@required VoidCallback onPressed}) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: FlatButton(
          onPressed: onPressed,
          child: Text(label)),
    );
  }
  
  Future<bool> checkStatus() async {
    cameraStatus = await Permission.camera.status;
    storageStatus = await Permission.storage.status;

    if (cameraStatus != PermissionStatus.granted ||
        storageStatus != PermissionStatus.granted) {
      requestPermission(Permission.camera);
      requestPermission(Permission.storage);
      return false;
    }

    return true;
  }

  Future<void> requestPermission(Permission permission) async =>
      await permission.request();
}
